/**
 * 
 * 	Sprite 类的扩展，增加了几个对象及功能，为组件中所有对象的基类
 *
 **/
  
package org.dstart.draw.csuper
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Rectangle;
	
	import org.dstart.draw.cutils.CSpriteFilter;

	public class CSprite extends Sprite 
	{
		private var m_sprHot:Sprite;	//	背景框对象，该对象主要用于显示一些遮照区，不做表现用
		private var m_sprMain:Sprite;	//	主体绘制用对象（所有对象在这上面进行绘制）
		private var m_bmpMain:Bitmap;	//	主体显示对象（用于外部调用时的显示）
		
		private var m_filter:CSpriteFilter;		//	动画效果
		
		private var m_alpha:Number;

		public function CSprite()
		{
			InitObject();
			InitValue();
			InitListener();
		}
		
		//	重写 alpha 属性
		public override function set alpha(value:Number):void
		{
			// super.alpha = value;
			m_alpha = value;
			
			if(value < 1)
			{
				var matrix:Array = new Array();
	            matrix = matrix.concat([1, 0, 0, 0, 0]); 
	            matrix = matrix.concat([0, 1, 0, 0, 0]); 
	            matrix = matrix.concat([0, 0, 1, 0, 0]);
	            matrix = matrix.concat([0, 0, 0, value, 0]);
	
				var t:ColorMatrixFilter = new ColorMatrixFilter(matrix);
				this.filters = [t];
			}
			else
			{
				this.filters = null;
			}			
		}
		
		public override function get alpha():Number
		{
			return m_alpha;
		}

		/** 返回透明遮片层 */
		public function get _hot():Sprite
		{
			return m_sprHot;
		}

		/**	返回用于绘制对象用的主体 Sprite */
		public function get _root():Sprite
		{
			return m_sprMain;
		}
		
		/** 返回位图对象 */
		public function get _bmp():Bitmap
		{
			return m_bmpMain;
		}
		
		//-------------------------------------------------------------------------
		
		/**	设置对象的 X Y 坐标位置 */
		public function SetPlace(x:Number, y:Number):void
		{
			this.x = x;
			this.y = y;
		}
		
		/** 设置缓存位置 */
		public function SetBmpPlace(xx:Number, yy:Number):void
		{
			_root.x = _bmp.x = _hot.x = x;
			_root.y = _bmp.y = _hot.y = y;
		}
		
		/** 开启位图模式 */
		public function OpenBitmap(ww:Number = 0, hh:Number = 0, isAlpha:Boolean = true):void
		{
			RushBitmap(ww, hh, isAlpha);
			
			
			// this.addChild(this._bmp);
		}
		
		/**	关闭位图模式 */
		public function CloseBitmap():void
		{
			_root.visible = true;
			
			if(_bmp)
			{
				_bmp.x = 0;
				_bmp.y = 0;

				if(_bmp.bitmapData)
				{
					_bmp.bitmapData.dispose();
				}
				_bmp.visible = false;
			}			
		}
		
		//	显示
		public function Show(model:Number = 0):void
		{			
			this.m_filter.Show(model);
		}
		
		//	隐藏
		public function Hidden():void
		{
			this.m_filter.Hidden();
		}

		//-----------------------------------------------------------------------

		/**	刷新位图对象，进行更新 */
		protected function RushBitmap(w:Number, h:Number, isAlpha:Boolean):void
		{
			if(_bmp)
			{
				if(_bmp.bitmapData)
				{
					_bmp.bitmapData.dispose();
				}
			}

			var rect:Rectangle = this._root.getBounds(this);
			
			var xx:Number = Math.floor(_bmp.x - _root.x);
			var yy:Number = Math.floor(_bmp.y - _root.y);;
			var ww:Number = rect.width + 2;//this.width + 20;//rect.width + 5;//this.width + 5;//rect.width + 5;
			var hh:Number = rect.height + 2;//this.height + 20;//rect.height + 5;//this.height + 5;//rect.height + 5;
			
			ww = (ww <= 0) ? 1 : ww;
			hh = (hh <= 0) ? 1 : hh;	
			
			ww = (w > 0) ? w : ww;
			hh = (h > 0) ? h : ww;
			
			ww = (ww > 2000) ? 2000 : ww;
			hh = (hh > 2000) ? 2000 : hh;
			
			// _root.x = 5;
			// _root.y = 5;
			
			_bmp.bitmapData = new BitmapData(Math.abs(ww + 10), Math.abs(hh + 10), isAlpha, 0xFFCCCC);
			_bmp.bitmapData.draw(this._root);//, null, null, null, new Rectangle(xx, yy, Math.abs(ww), Math.abs(hh)), true);

			// _root.x = 0;
			// _root.y = 0;
			
			// _bmp.x = -5;
			// _bmp.y = -5;
			
			_root.visible = false;
			_bmp.visible = true;
			
			// this.addChild(_bmp);
		}
		
		//	更新显示，多用于初始化时进行初始化时动作
		protected function InitDraw():void
		{			
		}
			
		//-----------------------------------------------------------------------
		
		/**	初始化对象 */
		private function InitObject():void
		{
			m_sprHot  = new Sprite();
			m_sprMain = new Sprite();
			m_bmpMain = new Bitmap();

			_bmp.smoothing = true;			
			
			this.addChild(_hot);
			this.addChild(_root);
			this.addChild(_bmp);			
			
			this.m_filter = new CSpriteFilter(this);
			
			this._root.cacheAsBitmap = true;
			this._hot.cacheAsBitmap = true;
			this._bmp.cacheAsBitmap = true;
						
			this.cacheAsBitmap = true;
		}
		
		/** 初始化相关变量 **/
		private function InitValue():void
		{
			m_alpha = 1;
		}
		
		/**	初始化事件侦听  **/
		private function InitListener():void
		{
			this.addEventListener(Event.ADDED_TO_STAGE, onInitShow);
		}
		
		private function onInitShow(e:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, onInitShow);

			InitDraw();
		}		
	}
}